/*
 * 作者:熊猫
 * 日期:2017 - 3 - 15 11 - 50 - 41
 *
 */

package net.lainiao.morethread.Chapter6.demo4;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/15.
 */
public class Event implements Delayed {
    private Date startDate;

    public Event(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        Date now=new Date();
        long diff=startDate.getTime()-now.getTime();
        return unit.convert(diff,TimeUnit.MICROSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        long result=this.getDelay(TimeUnit.NANOSECONDS);
        result=result-o.getDelay(TimeUnit.NANOSECONDS);
        if(result<0){
            return -1;
        }
        if(result>0){
            return 1;
        }
        return 0;
    }
}
